#include "dialog_newlistfile.h"
#include "ui_dialog_newlistfile.h"
#include <QMessageBox>
#include <QPushButton>
#include <QFileDialog>
#include <QTextStream>
#include "mainwindow.h"
#include "ui_mainwindow.h"


Dialog_newListFile::Dialog_newListFile(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_newListFile)
{
    ui->setupUi(this);

     ui->textEdit_addr->setText(" ");
    ui->textEdit_filename->setText(" ");

}

Dialog_newListFile::~Dialog_newListFile()
{
    delete ui;
}

void Dialog_newListFile::on_pushButton_clicked()
{
     quint16 index;

     QString fileName = QFileDialog::getOpenFileName(this,tr("打开"), ".",tr("*.mp3;;**.*"));
    QFile file(fileName); // 新建QFile对象
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        QMessageBox::warning(this, tr("多文档编辑器"),
                              tr("无法读取文件 %1:\n%2.")
                              .arg(fileName).arg(file.errorString()));
        return ; // 只读方式打开文件，出错则提示，并返回false
    }
    ui->textEdit_filename->setText(fileName);




/*
    index = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(index);
    ui->tableWidget->setItem(index,0,new QTableWidgetItem("lisi"));
    ui->tableWidget->setItem(index,1,new QTableWidgetItem("20"));
    ui->tableWidget->setItem(index,2,new QTableWidgetItem(fileName));*/
}

void Dialog_newListFile::on_pushButton_3_clicked()
{
    close();
}

void Dialog_newListFile::on_pushButton_2_clicked()
{
    MainWindow *fartherwindow = (MainWindow *)parentWidget();
    if(fartherwindow != NULL)
    {
       if(fartherwindow->CheckID(ui->spinBox_id->text())){
           QString filename = ui->textEdit_filename->toPlainText();
           if(filename.size()>0){
           fartherwindow->fileaddlist(ui->spinBox_id->text(),ui->textEdit_addr->toPlainText(),ui->textEdit_filename->toPlainText());
           ui->textEdit_filename->clear();
           close();
           }else{
               QMessageBox::warning(this, tr("提示"),tr("文件路径为空"));
           }

       }

    }
}
